<template>
  <div class="container">
    <div class="header">
      <div>
        <span class="title">群昵称：</span>
        <el-input v-model="search.name" placeholder="请输入内容" size="medium" />
      </div>

      <div>
        <span class="title">群主昵称：</span>
        <el-input v-model="search.owner_name" placeholder="请输入内容" size="medium" />
      </div>

      <div>
        <span class="title">创建时间：</span>
        <el-date-picker
          v-model="timer"
          size="medium"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
          :picker-options="pickerOptions"
          @change="selectTime"
        />
      </div>

      <div>
        <el-button size="small" type="primary" @click="reset">重置</el-button>
        <el-button size="small" type="primary" @click="getList">搜索</el-button>
      </div>
    </div>

    <div class="content">
      <el-table
        v-loading="loading"
        :data="list"
        :header-cell-style="{'color':'#000'}"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          width="100"
          align="center"
        />
        <el-table-column
          prop="im_id"
          label="群聊ID"
          width="200"
          align="center"
        />
        <el-table-column
          prop="name"
          label="群聊昵称"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            {{ scope.row.name || '—' }}
          </template>
        </el-table-column>
        <el-table-column
          label="群主信息"
          align="center"
          width="300"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.owner" class="owner">
              <img :src="scope.row.owner.avatar" alt="">
              <div>
                <h4>{{ scope.row.owner.nickname || '未填写昵称' }}</h4>
                <p>{{ scope.row.owner.phone }}</p>
              </div>
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>

        <el-table-column
          label="红包"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <div class="redPacket">
              <p>数量：{{ scope.row.send_count }}</p>
              <p>金额：{{ scope.row.send_total_money }}</p>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="创建时间"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="群功能" align="center" width="180">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.all_permissions_status" :active-value="1" :inactive-value="0" @change="changeStatus(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="lookMember(scope.row)"><i class="el-icon-view" style="margin-right: 4px;" />群成员</el-button>
            <el-button type="text" size="small" @click="lookFict(scope.row)"><i class="el-icon-edit" style="margin-right: 4px;" />虚拟成员</el-button>
            <el-button type="text" size="small" @click="lookRecord(scope.row)"><i class="el-icon-view" style="margin-right: 4px;" />红包记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pagesize"
          :page-sizes="[8, 10, 20, 30]"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 群成员 -->
    <Menber v-model="menberDialog" :group-id="groupId" :title="groupTitle" />

    <!-- 虚拟成员 -->
    <Fictitious v-model="fictitiousDialog" :group-id="groupId" :pub-vue="pubVue" @add="AddFictDialog=true" />

    <!-- 新增虚拟成员 -->
    <AddFict v-model="AddFictDialog" :group-id="groupId" :pub-vue="pubVue" />

    <!-- 红包记录 -->
    <Record v-model="recordDialog" :group-id="groupId" :title="groupTitle" />
  </div>
</template>

<script>
import Menber from './components/menber.vue'
import Record from './components/record.vue'
import Fictitious from './components/fictitious.vue'
import AddFict from './components/AddFict.vue'
import Vue from 'vue'
export default {
  components: { Menber, Record, Fictitious, AddFict },
  data() {
    return {
      pubVue: new Vue(),
      loading: false,
      timer: '',
      search: {
        owner_name: '',
        name: '',
        start_time: '',
        end_time: ''
      },

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },

      list: [],
      total: 0,
      pagesize: 8, // 默认一页6条
      currentPage: 1, // 当前页

      menberDialog: false,
      recordDialog: false,
      fictitiousDialog: false,
      AddFictDialog: false,
      groupId: '',
      groupTitle: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 重置
    reset() {
      this.currentPage = 1
      this.pagesize = 10
      this.search = {}
      this.timer = ''
      this.list = []
      this.getList()
    },

    // 获取表格数据
    getList() {
      this.loading = true
      this.$request.get('/admin/group/list', {
        params: {
          ...this.search,
          limit: this.pagesize,
          page: this.currentPage
        }
      }).then(res => {
        this.loading = false
        this.list = res.data.data
        this.total = res.data.total
      }).catch(_ => { this.loading = false })
    },

    // 设置每页条数
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 选择时间
    selectTime(e) {
      this.search.start_time = e[0]
      this.search.end_time = e[1]
    },

    // 修改状态
    changeStatus(e) {
      this.$request.post(`/admin/group/setting`, {
        status: e.all_permissions_status,
        group_id: e.im_id
      }).then(res => {

      }).catch()
    },

    // 查看群成员
    lookMember(e) {
      this.menberDialog = true
      this.groupId = e.im_id
      this.groupTitle = e.name
    },

    lookFict(e) {
      this.fictitiousDialog = true
      this.groupId = e.im_id
    },

    // 查看红包记录
    lookRecord(e) {
      this.recordDialog = true
      this.groupId = e.im_id
      this.groupTitle = e.name
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    width: 100%;
    height: auto;
    padding: 10px;
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    &>div{
      width: calc(100% / 4 - 20px);
      display: flex;
      align-items: center;

      &:nth-child(3){
        width: calc(100% / 4 + 60px);
      }
      &:nth-child(4){
        width: calc(100% / 4 - 100px);
      }
      &>.title{
        display: inline-block;
        width: 100px;
        font-size: 14px;
      }
    }
  }
  .content{
    width: 100%;
    margin-top: 10px;
  }
  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .btn{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    &>div:first-child{ margin-right: 20px; }
  }
  .owner{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    &>img{
      width: 60px;
      height: 60px;
      object-fit: cover;
      border-radius: 10px;
    }
    &>div{
      width: calc(100% - 80px);
    }
  }

  .redPacket{
    padding-left: 20px;
  }
  .look{
    font-size: 14px;
    color: #1989fa;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    &>span{ margin-left: 10px; }
  }
</style>
